﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CoD4_Helper
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
            // Events
            txtInstDir.Click += new EventHandler(txtOpenDir_Click);
            txtProfDir.Click += new EventHandler(txtOpenDir_Click);

            txtInstDir.TextChanged += new EventHandler(txtDir_TextChanged);
            txtProfDir.TextChanged += new System.EventHandler(txtDir_TextChanged);
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            txtInstDir.Text = Utils.COD.InstallationDir;
            txtProfDir.Text = Utils.COD.ProfilesDir;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // if the user has actually changed the path
            if (Utils.COD.InstallationDir != txtInstDir.Text)
            {
                Utils.SettingsManager.SetCustomInstDir(txtInstDir.Text);
                Utils.COD.InstallationDir = txtInstDir.Text;
            }

            if (Utils.COD.ProfilesDir != txtProfDir.Text)
            {
                Utils.SettingsManager.SetCustomProfDir(txtProfDir.Text);
                Utils.COD.ProfilesDir = txtProfDir.Text;
            }
            
            this.Dispose();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        //private void btnApply_Click(object sender, EventArgs e)
        //{
        //    CoDUtils.InstallationDir = txtInstDir.Text;
        //    CoDUtils.ProfilesDir = txtProfDir.Text;

        //    // save to persistant settings
        //    Properties.Settings.Default.userInstDir = txtInstDir.Text;
        //    Properties.Settings.Default.userProfilesDir = txtProfDir.Text;

        //    Properties.Settings.Default.Save();
        //}

        private void btnCheck_Click(object sender, EventArgs e)
        {
            //lblInstRes.Text = CoDUtils.DetectCODInstallation().ToString();
            //lblProfRes.Text = CoDUtils.DetectCODProfiles().ToString();
        }

        private void btnSetDefault_Click(object sender, EventArgs e)
        {
            txtInstDir.Text = Utils.COD.GetDefaultInstDir();
            txtProfDir.Text = Utils.COD.GetDefaultProfDir();
        }


        private void txtOpenDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.RootFolder = Environment.SpecialFolder.MyComputer;

            TextBox txt = (TextBox)sender;

            if (txt.Name == "txtInstDir")
            {
                fbd.Description = "Select the installation folder";
                if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    txtInstDir.Text = fbd.SelectedPath;
            }
            else
            {
                fbd.Description = "Select the profiles folder";
                if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    txtProfDir.Text = fbd.SelectedPath;
            }
        }

        void txtDir_TextChanged(object sender, EventArgs e)
        {
            if (Utils.COD.DetectCODInstallation(txtInstDir.Text))
            {
                btnOK.Enabled = true;
                lblInstRes.Text = "Installation path is correct";
            }
            else
            {
                btnOK.Enabled = false;
                lblInstRes.Text = "Installation path is wrong";
            }

            if (Utils.COD.DetectCODProfiles(txtProfDir.Text))
            {
                btnOK.Enabled = true;
                lblProfRes.Text = "Profiles path is correct";
            }
            else
            {
                btnOK.Enabled = false;
                lblProfRes.Text = "Profiles path is wrong";
            }

        }

        

        

        
        
    }
}
